<template>
  <div class="tools-li">
    <transition name="el-zoom-in-top">
      <div v-show="isShow" :key="tooltipId" ref="customAlert" class="custom-demo" :style="customDemo" transiton="el-zoom-in-center">
        <div
          class="custom-ul-li"
          :style="mouseId === 0 ? customMenu: ''"
          @mouseover="mousemove({id:0})"
          @mouseleave="mouseleave()"
          @click="doClick({id:0})"
        >默认主题</div>
        <div
          v-for="(item,index) in dataList"
          :key="'the-'+index"
          class="custom-ul-li"
          :style="mouseId === item.id ? customMenu: ''"
          @click="doClick(item)"
          @mouseover="mousemove(item)"
          @mouseleave="mouseleave()"
        >{{ item.name }}</div>
      </div>
    </transition>
    <slot name="theme" />
  </div>
</template>

<script>
import { userChecked } from '@/api/system/theme/user';
import { queryAllTheme } from '@/api/system/theme/index';
import { generateId } from 'element-ui/src/utils/util';
export default {
  name: 'Theme',
  props: {
    trigger: {
      type: String,
      default: 'click',
      validator: value => ['click', 'focus', 'hover', 'manual'].indexOf(value) > -1
    }
  },
  data() {
    return {
      isShow: false,
      customDemo: {},
      customMenu: {},
      dataList: [],
      mouseId: null
    };
  },
  computed: {
    themes() {
      return this.$store.getters.theme;
    },
    tooltipId() {
      return `el-custom-user-${generateId()}`;
    }
  },
  watch: {
    themes(val, oldVal) {
      this.customDemo.background = val.backGroundColor;
      this.customDemo.color = val.fontColor;
      this.customMenu.color = val.columnTitleFontColor;
      this.customMenu.backgroundColor = val.columnTitleGroundColor;
      this.customMenu.borderBottom = '1px solid ' + val.bkys;
    }
  },
  mounted() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.addEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.addEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.addEventListener('mouseleave', this.triggerClose);
    }
  },
  destroyed() {
    if (this.trigger === 'click' && undefined !== this.$refs.customAlert) { // click 事件
      this.$refs.customAlert.removeEventListener('click', this.triggerOnClick);
    }
    if (this.trigger === 'hover' && undefined !== this.$refs.customAlert) {
      this.$refs.customAlert.removeEventListener('mouseenter', this.triggerOpen);
      this.$refs.customAlert.removeEventListener('mouseleave', this.triggerClose);
    }
  },
  created() {
    this.initAllTheme();
  },
  methods: {
    doClick(data) {
      this.$confirm('确定切换为：【' + (data.name === undefined ? '默认主题' : data.name) + '】主题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let id = null;
        if (data.id !== 0) {
          id = data.id;
        }
        userChecked({ tid: id }).then(res => {
          this.$store.dispatch('GetThemes').then((res) => {
            this.$message.success('切换成功！');
          }).catch((error) => {
            console.log(error);
          });
        }).catch(() => {
          console.log('切换失败');
        });
      }).catch(() => {
        this.$message.success('取消成功！');
      });
    },
    initAllTheme() {
      queryAllTheme().then(res => {
        const { data } = res;
        this.dataList = data.data;
      }).catch(() => {
        this.dataList = [];
      });
    },
    triggerOnClick() {
      if (this.isShow) {
        this.isShow = false;
      } else {
        this.isShow = true;
      }
    },
    triggerOpen() {
      this.isShow = true;
      this.$emit('callBack', this.isShow);
    },
    triggerClose() {
      this.isShow = false;
      this.$emit('callBack', this.isShow);
    },
    mousemove(data) {
      this.mouseId = data.id;
    },
    mouseleave() {
      this.mouseId = null;
    }
  }
};
</script>

<style scoped>
.tools-li {
  width: 100%;
  height: 100%;
  position: relative;
}
.custom-demo {
  position: absolute;
  top: 60px;
  right: 0px;
  height: auto;
  padding: 10px;
  box-sizing: border-box;
  width: 120px;
  z-index: 100;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 4px;
}
.custom-ul-li {
  width: 100%;
  height: 40px;
  line-height: 40px;
  font-size: 0.8rem;
  cursor: pointer;
  text-align: center;
}
</style>
